package main

import (
	"fmt"
)

// 策略模式
// 商场促销 A策略8折 B策略满200返100
type SaleStrategy interface {
	CalculatePrice(price float32)
}

// SaleStrategyA 策略A
type SaleStrategyA struct {
}

func (s *SaleStrategyA) CalculatePrice(price float32) {
	fmt.Println(price * 0.8)
}

// SaleStrategyB 策略B
type SaleStrategyB struct {
}

func (s *SaleStrategyB) CalculatePrice(price float32) {
	if price >= 200 {
		price = price - 100
	}
	fmt.Println(price)
}

type NewStrategy struct {
	SaleStrategy SaleStrategy
}

func (s *NewStrategy) SetStrategy(choose string) {
	//设置策略
	//要使用指针设置成员变量
	var strategy SaleStrategy
	switch choose {
	case "A":
		strategy = &SaleStrategyA{}
	case "B":
		strategy = &SaleStrategyB{}
	}
	s.SaleStrategy = strategy
}

func (s *NewStrategy) UseStrategy(price float32) {
	s.SaleStrategy.CalculatePrice(price)
}

func main() {
	s := NewStrategy{}
	s.SetStrategy("A")
	s.UseStrategy(200)
	s.SetStrategy("B")
	s.UseStrategy(200)
}
